package com.authine.cloudpivot.common.extension.customized.controller;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.authine.cloudpivot.common.extension.utils.SsoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author hxd
 * @Date 2021/11/10 10:41
 * @Description
 **/

@Api(tags = "公共服务二开扩展服务::二开API")
@Slf4j
@RequestMapping("/custsso")
@RestController
public class CustSsoController {


    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @ApiOperation("获取corpId")
    @GetMapping("/getCorpId")
    public SingleResponse<String> getCorpId(String code) {

        String username = SsoUtils.getUsername(code);

        String sql = "select org_id as corpId from h_org_user_base oub left join h_org_user ou on oub.id = ou.user_base_id where oub.user_name = :username";

        Map<String, Object> map = new HashMap<>();
        map.put("username",username);

        String corpId = null;
        try {
            corpId = jdbcTemplate.queryForObject(sql, map, String.class);
        } catch (Exception e) {
            log.info("获取corpId 失败".concat(e.getMessage()),e);
            throw  new BizException("获取corpId 失败,系统异常");
        }

        if (corpId == null) {
            throw  new BizException("获取corpId 失败,corpId = null");
        }

        return SingleResponse.of(corpId);
    }



}
